import {
  getOutputSourceConfigs,
  updateOutputSourceConfig,
} from '@/services/OutputSourceSerivce';
import { create, destroy } from '@/services/PreviewService';
import { DisableType, OutputSourceConfig } from '@/types';
import { useModel } from '@@/exports';
import { useEffect, useMemo, useState } from 'react';
import { useRequest } from 'umi';
import { isNotBlank } from 'web-plugin-utils';

// @ts-ignore
export default () => {
  const [outputSourceConfigs, setOutputSourceConfigs] = useState<
    OutputSourceConfig[]
  >([]);

  // @ts-ignore
  const { initialState } = useModel('@@initialState');

  // @ts-ignore
  const { run: reloadOutputSourceConfigs } = useRequest<any>(
    () => getOutputSourceConfigs(`http://${initialState!.videoMatrixIp!}`),
    {
      manual: true,
      onSuccess(data) {
        setOutputSourceConfigs(data);
      },
    },
  );

  useEffect(() => {
    if (initialState) {
      if (isNotBlank(initialState!.videoMatrixIp!)) {
        reloadOutputSourceConfigs();
      }
    }
  }, [initialState?.videoMatrixIp]);

  //启用的显示器
  const enabledOutputSourceConfigs = useMemo(() => {
    return (outputSourceConfigs || []).filter(
      //disabled手动禁用 enabled服务端是否启用
      (c: OutputSourceConfig) => c.disabled === DisableType.Off && c.enabled,
    );
  }, [outputSourceConfigs]);

  /**
   * 更新显示器设置
   * @param outputSourceConfigs
   */
  const updateOutputSourceConfigs = async (
    outputSourceConfigs: OutputSourceConfig[],
  ) => {
    await updateOutputSourceConfig(
      `http://${initialState!.videoMatrixIp!}`,
      outputSourceConfigs,
    );
    await reloadOutputSourceConfigs();
  };

  /**
   * 销毁windows
   * @param outputSourceName 输出源名称
   */
  const destroyAllPreviewWindow = async (outputSourceName: string) => {
    await destroy(`http://${initialState!.videoMatrixIp!}`, outputSourceName);
  };

  /**
   * 创建预览window
   * @param outputSourceName
   */
  const createAllPreviewWindow = async (outputSourceName: string) => {
    await create(`http://${initialState!.videoMatrixIp!}`, outputSourceName);
  };
  /**
   * 更新显示器设置
   * @param config
   */
  const proxyUpdateOutputSourceConfig = async (config: OutputSourceConfig) => {
    if (config.splitScreenMode === '1x1') {
      config.previewConfig[1] =
        config.previewConfig[2] =
        config.previewConfig[3] =
          undefined;
    } else if (
      config.splitScreenMode === '1x2' ||
      config.splitScreenMode === 'LEFT1xRIGHT_BOTTOM1'
    ) {
      config.previewConfig[2] = config.previewConfig[3] = undefined;
    }

    // eslint-disable-next-line eqeqeq
    if (config.previewConfig.filter((i) => i != undefined).length === 0) {
      await destroyAllPreviewWindow(config.workerName);
    }
    const configIndex = outputSourceConfigs.findIndex(
      (c: OutputSourceConfig) => c.workerName === config.workerName,
    );
    if (configIndex !== -1) {
      outputSourceConfigs[configIndex] = config;
      await updateOutputSourceConfigs(outputSourceConfigs);
    }
  };

  /**
   * 更新输出配置，并自动创建和销毁预览窗口
   * @param config
   */
  const updatePlayback = async (config: OutputSourceConfig) => {
    await proxyUpdateOutputSourceConfig(config);
    await createAllPreviewWindow(config.workerName);
  };

  /**
   * 输出
   * @param inputSourceName
   * @param index
   * @param config
   */
  const playback = async (
    inputSourceName: string,
    index: number,
    config: OutputSourceConfig,
  ) => {
    // eslint-disable-next-line eqeqeq
    if (config.previewConfig == undefined) {
      config.previewConfig = [undefined, undefined, undefined, undefined];
    }

    config.previewConfig[index] = {
      inputSourceName,
      previewWindowId: `PW${index}`,
    };

    await updatePlayback(config);
  };

  return {
    outputSourceConfigs,
    enabledOutputSourceConfigs,
    updateOutputSourceConfig: proxyUpdateOutputSourceConfig,
    updateOutputSourceConfigs,
    reloadOutputSourceConfigs,
    playback,
    updatePlayback,
  };
};
